function trim(s) {
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;


   if(fld.value != ""){
       if (!emailFilter.test(tfld)) {              //test email for illegal characters
            fld.style.background = '#99ccff';
            error = "Introduzca una direccion de email valida.\n";
        } else if (fld.value.match(illegalChars)) {
            fld.style.background = '#99ccff';
            error = "El email contiene caracteres ilegales.\n";
        } else {
            fld.style.background = 'White';
        }
    }
    return error;
}

function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = '#99ccff';
        switch (fld.name){
            case "CLT_RAZON_SOCIAL":
                error = "El campo cedula no ha sido llenado.\n";
                break;

            case "CLT_NOMBRE":
                error = "El campo nombre no ha sido llenado.\n";
                break;

            case "CLT_APELLIDO":
                error = "El campo apellido no ha sido llenado.\n";
                break;

            case "CLT_EMAIL":
                error = "El campo email no ha sido llenado.\n";
                break;

                
        }

    } else {
        fld.style.background = 'White';
    }
    return error;
}

 function acceptNum(evt,nav4,opt)
 {
   var key = nav4 ? evt.which : evt.keyCode;
   if (opt == 1) return (key == 13 || key == 47 || (key >= 48 && key <= 57) || (key == 8) || (key == 9) || (key == 11));
   else return (key == 13 || key == 46 || (key >= 48 && key <= 57) || (key == 8) || (key == 9) || (key == 11));
 }

 function acceptChar(evt,nav4,opt) {
   var key = nav4 ? evt.which : evt.keyCode;
   if (opt == 1) return (key == 13 || key == 32 || key == 47 || (key >= 97 && key <= 122) || (key >= 65 && key <= 90) || (key == 8) || (key == 9) || (key == 11));
   else return (key == 13 || key == 32 || (key >= 97 && key <= 122) || (key >= 65 && key <= 90) || (key == 8) || (key == 9) || (key == 11));
 }

  function acceptTamano(valor,tamano) {
   if (valor.length == tamano){
       return false;
   }
   else{
       return true;
   }
 }

function validaAgregarCliente(theForm){
	var reason = "";

        if( ( theForm.CLT_TELEFONO.value.length > 0 && theForm.CLT_TELEFONO.value.length < 7 ) || theForm.CLT_TELEFONO.value.length > 13 ) {
            reason += "El campo telefono debe contener entre 7 y 13 digitos.\n";
            theForm.CLT_TELEFONO.style.background = '#99ccff';
	} else {
            theForm.CLT_TELEFONO.style.background = 'White';
        }

        reason += validateEmpty(theForm.CLT_RAZON_SOCIAL);
	reason += validateEmpty(theForm.CLT_NOMBRE);
	reason += validateEmpty(theForm.CLT_APELLIDO);
        reason += validateEmail(theForm.CLT_EMAIL);

	if (reason != "") {
            alert("Algunos campos necesitan correcion:\n" + reason);
            return false;
  	}

        return true;
}

function confirmarEliminarCliente(CLT_ID){
    if (confirm("Esta segunro que desea ELIMINAR el CLIENTE??")){
        document.location.href = 'EliminarCliente.php?CLT_ID='+CLT_ID;
    }
}